package com.hyf.serviceedu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hyf.service.config.exception.GuliException;
import com.hyf.serviceedu.Microservice.VodClient;
import com.hyf.serviceedu.entity.EduVideo;
import com.hyf.serviceedu.entity.condition.VideoInfoForm;
import com.hyf.serviceedu.mapper.EduVideoMapper;
import com.hyf.serviceedu.service.EduVideoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程视频 服务实现类
 * </p>
 *
 * @author atguigu
 * @since 2021-04-21
 */
@Service
public class EduVideoServiceImpl extends ServiceImpl<EduVideoMapper, EduVideo> implements EduVideoService {


    // 注入vodClient
    @Autowired
    private VodClient vodClient;


    @Override
    public void saveVideoInfo(VideoInfoForm videoInfoForm) {
        EduVideo video = new EduVideo();
        BeanUtils.copyProperties(videoInfoForm, video);
        boolean result = this.save(video);
        if(!result){
            throw new GuliException(20001, "课时信息保存失败");
        }
    }

    @Override
    public VideoInfoForm getVideoInfoFormById(String id) {
        //从video表中取数据
        EduVideo video = this.getById(id);
        if(video == null){
            throw new GuliException(20001, "数据不存在");
        }
        //创建videoInfoForm对象
        VideoInfoForm videoInfoForm = new VideoInfoForm();
        BeanUtils.copyProperties(video, videoInfoForm);
        return videoInfoForm;
    }
    @Override
    public void updateVideoInfoById(VideoInfoForm videoInfoForm) {
        //保存课时基本信息
        EduVideo video = new EduVideo();
        BeanUtils.copyProperties(videoInfoForm, video);
        boolean result = this.updateById(video);
        if(!result){
            throw new GuliException(20001, "课时信息保存失败");
        }
    }


    // 根据课程id 删除小结
    // TODO 删除小结，还要三次小结对应的视频
    @Override
    public void removeVideoByIdCourseId(String courseId) {


        // 1.根据课程id查询出视频id
        QueryWrapper<EduVideo> wrapperVideo=new QueryWrapper<>();

        wrapperVideo.eq("course_id",courseId);
        wrapperVideo.select("video_source_id");// 查询指定字段的名字
        // 调用baseMapper 中的方法实现一个查询
        List<EduVideo> eduVideoList = baseMapper.selectList(wrapperVideo);

        // 取出 List<EduVideo>  里面的值 转换为 List<String> 的形式
        List<String> videoList=new ArrayList<>();
        // 使用 for 循环遍历一下
        for (int i=0;i<eduVideoList.size(); i++){
            // 拿到EduVideo 对象
            EduVideo eduVideo=eduVideoList.get(i);
            // 然后再从 eduvideo 里面取出来
            String videoSourceId = eduVideo.getVideoSourceId();
            // 判断 视频id 值是否为空
            if(!StringUtils.isEmpty(videoSourceId)){

                // 放到 videoList 里面去
                videoList.add(videoSourceId);
            }

        }


        // 判断 视频id 的集合也不为空
        if(videoList.size() > 0){
            // 根据多个视频id 删除视频 多个视频
            vodClient.deleteBatch(videoList);
        }





        QueryWrapper<EduVideo> wrapper=new QueryWrapper<>();
        wrapper.eq("course_id",courseId);
        baseMapper.delete(wrapper);

    }



//    // 课件中的方法
//    @Override
//    public boolean removeVideoById(String id) {
//        //查询云端视频id
//        EduVideo eduVideo = baseMapper.selectById(id);
//        String videoSourceId = eduVideo.getVideoSourceId();
//        //删除视频资源
//        if(!StringUtils.isEmpty(videoSourceId)){
//            vodClient.removeVideo(videoSourceId);
//        }
//        Integer result = baseMapper.deleteById(id);
//        return null != result && result > 0;
//    }


}
